﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PingTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private List<NetNode> nodes = new List<NetNode>();

        private void btnAdd_Click(object sender, EventArgs e)
        {
            addNode(tbName.Text.Trim(),tbIP.Text.Trim());                        
            tbIP.Text = "";
            tbName.Text = "";
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(openFileDialog1.OpenFile());
                string[] tokens;
                string line, name, ip;
                while ((line = sr.ReadLine()) != null)
                {
                    tokens = line.Split(',');
                    if (tokens.Length > 1)
                    {
                        name = tokens[0];
                        ip = tokens[1];
                        addNode(name, ip);
                    }
                }
            }
        }

        private void addNode(string name, string ip)
        {
            NetNode n = new NetNode(ip);
            nodes.Add(n);
            n.name = name;
            dataGridView1.Rows.Add(1);
            n.row = dataGridView1.Rows[dataGridView1.Rows.Count - 1];
            n.row.Cells[0].Value = nodes.Count;
            n.row.Cells[1].Value = n.name;
            if (n.ip != null)
                n.row.Cells[1].Value += " (" + n.ip.ToString()  + ")";
            else
                n.row.Cells[1].Value += " (" + n.dns + ")";            
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            timer1.Start();
            btnStart.Enabled = false;
            btnStop.Enabled = true;
            btnAdd.Enabled = false;
            btnImport.Enabled = false;
            btnConfig.Enabled = false;
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            timer1.Stop();
            timer1.Enabled = false;
            btnStop.Enabled = false;
            btnStart.Enabled = true;
            btnAdd.Enabled = true;
            btnImport.Enabled = true;
            btnConfig.Enabled = true;
            //
            foreach (NetNode n in this.nodes)
            {                
                n.StopPing();                
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            foreach (NetNode n in this.nodes)
            {
                // 刷新表格
                n.RefreshRow();
                n.StartPing();
            }
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            ConfigForm cf = new ConfigForm();
            if (cf.ShowDialog() == DialogResult.OK)
            {
                this.Text = Config.MAIN_FORM_TITLE;
                if (Config.PING_INTERVAL < 200)
                    Config.PING_INTERVAL = 200;
                timer1.Interval = Config.PING_INTERVAL;
                Config.PING_TIME_OUT = Convert.ToInt32(Config.PING_INTERVAL * 0.95);
                if (Config.MAX_BUFFER_SIZE < 256)
                    Config.MAX_BUFFER_SIZE = 256;
            }
        }    
    }
}
